package cn.ffcs.uec.staff.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.staff.action.bean.UecStaffInfoEditBean;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;

@Controller
@Scope("prototype")
public class UecStaffInfoEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecStaffInfoEditBean bean = new UecStaffInfoEditBean();

	private UecStaffInfo uecStaffInfo;
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	@SuppressWarnings("unused")
	private UecStaffInfo uecStaff;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 * @throws ParseException 
	 */
	public void onCreate$uecStaffInfoEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定下拉框 绑定combobox
	 */
	private void bindCombobox() {
		//绑定证件类型
		
//		List<NodeVo> nodeVoIdTypeList = new ArrayList<NodeVo>();
//		String[][] idType = {{"0", "身份证"}};
//		for (int i = 0; i < idType.length; i++) {
//			NodeVo nodeVo = new NodeVo();
//			for (int j = 0; j < 1; j++) {
//				nodeVo.setId(idType[i][j]);
//				nodeVo.setName(idType[i][j + 1]);
//			}
//			nodeVoIdTypeList.add(nodeVo);
//		}
//		ListboxUtils.rendererForEdit(bean.getIdtype(), nodeVoIdTypeList);
		
		// 绑定性别sex:
		List<NodeVo> nodeVoSexList = new ArrayList<NodeVo>();
		String[][] sex = {{"1", "男"}, {"2", "女"}, {"3", "未知"}};
		for (int i = 0; i < sex.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(sex[i][j]);
				nodeVo.setName(sex[i][j + 1]);
			}
			nodeVoSexList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getSex(), nodeVoSexList);

		// 绑定民族nationality
		List<NodeVo> nodeVoNationalityList = new ArrayList<NodeVo>();
		String[][] nationality = {{"1001A11000000000063W", "汉族"},
				{"1001A110000000000647", "侗族"}, {"1001A11000000000063Y", "回族"},
				{"1001A110000000000646", "满族"}, {"1001A11000000000063X", "蒙古族"}};
		for (int i = 0; i < nationality.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(nationality[i][j]);
				nodeVo.setName(nationality[i][j + 1]);
			}
			nodeVoNationalityList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getNationality(),
				nodeVoNationalityList);

		// 婚姻状况marital
		List<NodeVo> nodeVoMaritalList = new ArrayList<NodeVo>();
		String[][] marital = {{"1001A1100000000005XQ", "未婚"},
				{"1001A1100000000005XR", "已婚"}, {"1001A1100000000005XS", "再婚"},
				{"1001A1100000000005XT", "离异"}, {"1001A1100000000005XU", "丧偶"}};
		for (int i = 0; i < marital.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(marital[i][j]);
				nodeVo.setName(marital[i][j + 1]);
			}
			nodeVoMaritalList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getMarital(), nodeVoMaritalList);

		// 健康状况
		List<NodeVo> nodeVoHealthList = new ArrayList<NodeVo>();
		String[][] health = {{"1001A1100000000005YD", "健康"},
				{"1001A1100000000005YE", "一般"},
				{"1001A1100000000005YF", "有慢性病"},
				{"1001A1100000000005YG", "有生理缺陷"},
				{"1001A1100000000005YH", "残疾"}, {"1001A1100000000005YI", "较差"}};
		for (int i = 0; i < health.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(health[i][j]);
				nodeVo.setName(health[i][j + 1]);
			}
			nodeVoHealthList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getHealth(), nodeVoHealthList);

		// 政治面貌
		List<NodeVo> nodeVoPolityList = new ArrayList<NodeVo>();
		String[][] polity = {{"1001A1100000000006D6", "中国共产党党员"},
				{"1001A1100000000006D7", "中国共产党预备党员"},
				{"1001A1100000000006D8", "中国共产主义青年团团员"},
				{"1001A1100000000006DI", "群众"}};
		for (int i = 0; i < polity.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(polity[i][j]);
				nodeVo.setName(polity[i][j + 1]);
			}
			nodeVoPolityList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getPolity(), nodeVoPolityList);

		// 学历edu
		List<NodeVo> nodeVoEduList = new ArrayList<NodeVo>();
		String[][] edu = {{"1001A1100000000006AN", "硕士研究生"},
				{"1001A1100000000006AO", "研究生同等学历"},
				{"1001A1100000000006AP", "大学本科"},
				{"1001A1100000000006AQ", "专科"}, {"1001A1100000000006AR", "中专"},
				{"1001A1100000000006AS", "高中"},
				{"1001A1100000000006AT", "技工学校"},
				{"1001A1100000000006AU", "初中"}, {"1001A1100000000006AV", "小学"},
				{"1001A1100000000006AW", "无"},
				{"1001A1100000000006AX", "大学普通班"}};
		for (int i = 0; i < edu.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(edu[i][j]);
				nodeVo.setName(edu[i][j + 1]);
			}
			nodeVoEduList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getEdu(), nodeVoEduList);

		// 学位pkDegree
		List<NodeVo> nodeVoPkDegreeList = new ArrayList<NodeVo>();
		String[][] pkDegree = {{"1001A1100000000006AY", "博士"},
				{"1001A1100000000006AZ", "双硕士"},
				{"1001A1100000000006B0", "硕士"},
				{"1001A1100000000006B1", "双学士"},
				{"1001A1100000000006B2", "学士"}, {"1001A1100000000006B3", "无"}};
		for (int i = 0; i < pkDegree.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(pkDegree[i][j]);
				nodeVo.setName(pkDegree[i][j + 1]);
			}
			nodeVoPkDegreeList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getPkDegree(), nodeVoPkDegreeList);

		// 户口性质characterrpr
		List<NodeVo> nodeVoCharacterrprList = new ArrayList<NodeVo>();
		String[][] characterrpr = {{"1001A1100000000005XM", "城镇"},
				{"1001A1100000000005XN", "农村"}};
		for (int i = 0; i < characterrpr.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(characterrpr[i][j]);
				nodeVo.setName(characterrpr[i][j + 1]);
			}
			nodeVoCharacterrprList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getCharacterrpr(),
				nodeVoCharacterrprList);

		// 是否是历史成员
		List<NodeVo> nodeVoIshiskeypsnList = new ArrayList<NodeVo>();
		String[][] ishiskeypsn = {{"Y", "是"}, {"N", "否"}};
		for (int i = 0; i < ishiskeypsn.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(ishiskeypsn[i][j]);
				nodeVo.setName(ishiskeypsn[i][j + 1]);
			}
			nodeVoIshiskeypsnList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getIshiskeypsn(),
				nodeVoIshiskeypsnList);
		// 是否是门店营业员
		ListboxUtils.rendererForEdit(bean.getIsshopassist(),
				nodeVoIshiskeypsnList);
		// 是否承包经营个人
		ListboxUtils.rendererForEdit(bean.getGlbdef14(), nodeVoIshiskeypsnList);

	}
	/**
	 * bindBean
	 * @throws ParseException 
	 */
	public void bindBean() throws ParseException {
		opType = (String) arg.get("opType");
		if ("add".equals(opType)) {
			this.bean.getUecStaffInfoEditWindow().setTitle("人员基本信息新增");
			uecStaffInfo = (UecStaffInfo) arg.get("uecStaffInfo");
			if (null != uecStaffInfo) {
				PubUtil.fillBeanFromPo(uecStaffInfo, bean);
				bean.getIdtype().setValue("身份证");
				stringTransToDate();
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecStaffInfoEditWindow().setTitle("人员基本信息编辑");
			uecStaffInfo = (UecStaffInfo) arg.get("uecStaffInfo");
			if (null != uecStaffInfo) {
				PubUtil.fillBeanFromPo(uecStaffInfo, bean);
				bean.getIdtype().setValue("身份证");
				stringTransToDate();
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		if ("add".equals(opType)) {
			uecStaffInfo = UecStaffInfo.newInstance();
			PubUtil.fillPoFromBean(bean, uecStaffInfo);
			// 一套模型，两套标准：日期类型需要格式化
			dateTransToString();
			uecStaffInfo.addOnly();
			Events.postEvent("onOK", this.self, uecStaffInfo);
			this.onCancel();
			Messagebox.show("新增成功！");
		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecStaffInfo);
			dateTransToString();
			uecStaffInfo.updateOnly();
			Events.postEvent("onOK", this.self, uecStaffInfo);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecStaffInfoEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		// if (StrUtil.isNullOrEmpty(bean.getEncourName().getValue())) {
		// return "填写奖励名称";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourDate().getValue())) {
		// return "填写奖励日期";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourType().getValue())) {
		// return "填写奖励类型";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourMeas().getValue())) {
		// return "填写奖励措施";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourOrg().getValue())) {
		// return "填写奖励机构";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourRank().getValue())) {
		// return "填写奖励级别";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourGrade().getValue())) {
		// return "填写奖励档次";
		// }
		return null;
	}
	/**
	 * 将Date类型时间转成String类型
	 * Date --> String
	 * 一套模型，两套标准：日期类型需要格式化
	 */
	public void dateTransToString() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		if (!StrUtil.isNullOrEmpty(bean.getJoinpolitydate().getValue())) {
			// 入党(团)日期
			uecStaffInfo.setJoinpolitydate(formatter.format(bean
					.getJoinpolitydate().getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getBirthdate().getValue())) {
			// 出生日期
			uecStaffInfo.setBirthdate(formatter.format(bean.getBirthdate()
					.getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getDieDate().getValue())) {
			// 身故日期
			uecStaffInfo.setDieDate(formatter.format(bean.getDieDate()
					.getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getJoinworkdate().getValue())) {
			// 参加工作日期
			uecStaffInfo.setJoinworkdate(formatter.format(bean
					.getJoinworkdate().getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getRetiredate().getValue())) {
			// 离退休日期
			uecStaffInfo.setRetiredate(formatter.format(bean.getRetiredate()
					.getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getGlbdef15().getValue())) {
			// 预计退休日期
			uecStaffInfo.setGlbdef15(formatter.format(bean.getGlbdef15()
					.getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getGlbdef3().getValue())) {
			// 进入电信（邮电）系统日期
			uecStaffInfo.setGlbdef3(formatter.format(bean.getGlbdef3()
					.getValue()));
		}
	}
	
	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		// 出生日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (null != uecStaffInfo.getBirthdate()) {
			bean.getBirthdate()
					.setValue(sdf.parse(uecStaffInfo.getBirthdate()));
		}
		// 入党(团)日期
		if (!StrUtil.isNullOrEmpty(uecStaffInfo.getJoinpolitydate())) {
			bean.getJoinpolitydate().setValue(
					sdf.parse(uecStaffInfo.getJoinpolitydate()));
		}
		// 身故日期
		if(!StrUtil.isNullOrEmpty(uecStaffInfo.getDieDate())){
			bean.getDieDate().setValue(sdf.parse(uecStaffInfo.getDieDate()));
		}
		// 参加工作日期
		if(!StrUtil.isNullOrEmpty(uecStaffInfo.getJoinworkdate())){
			bean.getJoinworkdate().setValue(sdf.parse(uecStaffInfo.getJoinworkdate()));
		}
		// 离退休日期
		if(!StrUtil.isNullOrEmpty(uecStaffInfo.getRetiredate())){
			bean.getRetiredate().setValue(sdf.parse(uecStaffInfo.getRetiredate()));
		}
		// 预计退休日期
		if(!StrUtil.isNullOrEmpty(uecStaffInfo.getGlbdef15())){
			bean.getGlbdef15().setValue(sdf.parse(uecStaffInfo.getGlbdef15()));
		}
		// 进入电信（邮电）系统日期
		if(!StrUtil.isNullOrEmpty(uecStaffInfo.getGlbdef3())){
			bean.getGlbdef3().setValue(sdf.parse(uecStaffInfo.getGlbdef3()));
		}
		
	}

}
